/*
 * @Description: 描述
 * @Author: cy
 * @Date: 2024/5/12
 */

const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');
const directoryPathAll = path.join(__dirname, `./0214`); // 替换为你的目录路径
const files0214 = fs.readdirSync(directoryPathAll);



files0214.forEach((file)=>{
  console.log(13,file,__dirname)

  let f=`/0214/${file}`
  const directoryPath = path.join(__dirname, `./${f}`); // 替换为你的目录路径

  //判断是不是文件夹，如果是单个文件（）-不能遍历
  // Error: ENOTDIR: not a directory, scandir '/Users/chaoyang/cy/cy/media/code/react/reactCli/cy_server/0214/.DS_Store'
  const stats = fs.statSync(directoryPath);
  if(!stats.isDirectory()){
    return
  }


  let arr=[]
  const files = fs.readdirSync(directoryPath);
  files.forEach((file) => {
    console.log(file);
    if(file?.includes('ts')){
      let url=`file '${__dirname}/${f}/${file}'`
      // let url=`file '/Users/chaoyang/cy/cy/media/code/react/reactCli/cy_server/${f}/${file}'`
      arr.push(url)
    }
  });
  fs.writeFile(`./${file}.txt`, arr.join('\n'), err => {
    if(err) {
      console.log('写入失败')
    } else {
      console.log('写入成功');
    }
  })
  let execCommand=`ffmpeg -f concat -safe 0 -i ${file}.txt -c copy  -bsf:a aac_adtstoasc -movflags +faststart -y o${file}.mp4`
  exec(execCommand, (error, stdout, stderr) => {
    if (error) {
      console.error(`执行的错误: ${error}`);
      return;
    }
    console.log(`stdout: ${stdout}`);
    if (stderr) {
      console.log(`stderr: ${stderr}`);
    }
  });

})